package com.cloud.common.interceptor;

import io.jsonwebtoken.Jwts;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;

/**
 * @Description:
 * @author: lwg
 * @createTime: 2019年11月20日 16:10:00
 */
public class GetLoginUserInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String header = request.getHeader("Authorization");
        if (header == null) {
            response.getWriter().println();
        }


        String token = StringUtils.substringAfter(header, "bearer ");
        Jwts.parser().setSigningKey("test_key".getBytes(StandardCharsets.UTF_8)).parseClaimsJws(token).getBody();

        return super.preHandle(request, response, handler);
    }
}
